package edu.bbk.muc.lifetracker.sensormanager;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.util.Log;
import edu.bbk.muc.lifetracker.common.Configuration;
import edu.bbk.muc.lifetracker.common.SensorsToUi;

public class LightRecorder extends SensorRecorder {
  /**
   * Listener for this {@link LightRecorder} events.
   * @author irenatyler and romankirillov
   */
  class LightSensorListener extends AbstractSensorListener {
    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
      Log.d(TAG, "SensorValue: " + sensorEvent.values[0]);
      getSensorsToUi().onAmbientLightChanged(sensorEvent.timestamp, sensorEvent.values[0]);
    }
  }

  private static final String TAG = "LightRecorder";
  private static final int sensorType = Sensor.TYPE_LIGHT;

  /**
   * Initialises this and parent class fields.
   * @param currentActivity Base application activity
   * @param callback Callback receiver for all sensor events
   */
  public LightRecorder(Activity currentActivity, SensorsToUi callback) {
    super(currentActivity, callback);
    this.sensor = getSensorManager().getDefaultSensor(sensorType);
    Log.i(getTag(), "LightRecorder created");
  }

  @Override
  public void acceptNewConfiguration(Configuration configuration) {
    if (configuration.isAmbientLightEnabled()) {
      doStart();
    } else {
      doStop();
    }
  }

  @Override
  public AbstractSensorListener getListener() {
    if (this.listener == null) {
      setListener(new LightSensorListener());
    }
    return this.listener;
  }

  @Override
  public int getSensorType() {
    return sensorType;
  }

  @Override
  protected Sensor getSensor() {
    return this.sensor;
  }

  @Override
  protected String getTag() {
    return TAG;
  }

  @Override
  protected void setListener(AbstractSensorListener listener) {
    this.listener = listener;
  }
}
